package com.chat.datea.ui.musicpupwindow;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chat.datea.R;
import com.chat.datea.adapter.PupWindow_ListView_Adapter;
import com.chat.datea.bean.MusicList;
import com.chat.datea.ui.MainActivity;

import java.util.ArrayList;

public class MusicPupWindow extends PopupWindow {

    private ImageView type;
    private View mMenuView;
    private TextView clean, text;
    private ListView puplist;
    private PupWindow_ListView_Adapter adapter;


    private static final String modeName[] = {"列表循环", "顺序播放", "随机播放", "单曲循环"};
    private int modeDrawable[] = {R.drawable.bt_list_roundplay,
            R.drawable.bt_list_order, R.drawable.bt_list_random,
            R.drawable.bt_list_roundsingle};


    public MusicPupWindow(Activity context, OnClickListener itemsOnClick, final ArrayList<MusicList> lists, int position,int typeId) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_music, null);
        type = (ImageView) mMenuView.findViewById(R.id.type);
        type.setImageResource(modeDrawable[typeId]);
        clean = (TextView) mMenuView.findViewById(R.id.clean);
        puplist = (ListView) mMenuView.findViewById(R.id.puplist);
        text = (TextView) mMenuView.findViewById(R.id.text);
        if (lists.size() == 0) {
            text.setVisibility(View.VISIBLE);
        } else {
            text.setVisibility(View.GONE);
        }

        clean.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<MusicList> lists = new ArrayList<>();
                Log.e("MusicPupWindow", "lists:" + lists);
                MainActivity.play(lists, 0);
                dismiss();
                MainActivity.clean();
            }
        });
        type.setOnClickListener(itemsOnClick);
        adapter = new PupWindow_ListView_Adapter(context, lists, position);

        puplist.setAdapter(adapter);
        puplist.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MainActivity.play(lists, position);

            }
        });


        //设置PopupWindow的View
        this.setContentView(mMenuView);
        //设置PopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.MATCH_PARENT);
        //设置PopupWindow弹出窗体的高
        this.setHeight(LayoutParams.WRAP_CONTENT);
        //设置PopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (y < height) {
                        dismiss();
                    }
                }
                return true;
            }
        });

    }

    public void add(int position) {
        adapter.addPosition(position);
    }

    public ImageView getImg() {
        return type;
    }

}
